package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.*;
import com.javasm.service.impl.GameServiceImpl;
import com.javasm.service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/20 10:48
 * @since JDK 1.8
 */
@WebServlet("/game")
public class GameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        Map<String, Object> returnData = new HashMap<>();
        PageInfo pageInfo = null;
        List<AdminMenu> adminMenus = getMenu(req, resp);
        if ("getGameList".equals(method)) {
            pageInfo = getGameList(req, resp);
        } else if ("getGameById".equals(method)) {
            pageInfo = getGameById(req, resp);
        }
        AdminUser loginAccount = (AdminUser) req.getSession().getAttribute("loginAccount");
        // 判断查询条件
        if (pageInfo.getGameList().size() == 0) {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnMsg", ReturnCode.ERROR.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnAccount", loginAccount);
            returnData.put("returnData", adminMenus);
            returnData.put("pageInfo", pageInfo);
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 根据游戏id查询游戏信息，编辑界面使用
    private PageInfo getGameById(HttpServletRequest req, HttpServletResponse resp) {
        String gid = req.getParameter("gid");
        ArrayList<Game> gameArrayList = new ArrayList<>();
        gameArrayList.add(new GameServiceImpl().getGameById(gid));
        PageInfo pageInfo = new PageInfo();
        pageInfo.setGameList(gameArrayList);
        return pageInfo;
    }

    // 通过页码和每页条数查询游戏菜单信息
    private PageInfo getGameList(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        return new GameServiceImpl().selectGameByPage(page, pageSize);
    }

    // 根据登录的账户信息侧边栏展示可操作的菜单列表
    private List<AdminMenu> getMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminUser adminUser = (AdminUser) req.getSession().getAttribute("loginAccount");
        return new MenuServiceImpl().selectList(adminUser.getMenu_id());
    }
}
